﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IHISCETeim.DAL;
using IHISCETeim.Model;
using IHISCETeim.Library;
using System.Data;

namespace IHISCETeim.BLL
{
    public enum SMSKey
    { 
        生日短信模板
    }
   
    public class USMSTempleteController : BaseController
    {
        public static List<USMSTempleteInfo> GetAll()
        {
            List<USMSTempleteInfo> list=DC.Get<List<USMSTempleteInfo>>(USMSTempleteALLKey);
            if (list == null)
            {
                list = CBO.FillCollection<USMSTempleteInfo>(USMSTempleteDAL.GetAll());
                DC.Add(USMSTempleteALLKey, list);
            }
            return list;
        }
        public static bool Update(string Tkey, string SMSContent)
        {
            DC.Remove(USMSTempleteALLKey);
            return USMSTempleteDAL.Update(Tkey, SMSContent);
        }

        public static bool Add(string Tkey, string SMSContent)
        {
            DC.Remove(USMSTempleteALLKey);
            return USMSTempleteDAL.Add(Tkey, SMSContent);
        }
        /// <summary>
        /// 根据KEY获取内容
        /// </summary>
        /// <param name="Tkey"></param>
        /// <returns></returns>
        public static USMSTempleteInfo GetByKey(SMSKey Tkey)
        {
            List<USMSTempleteInfo> list = GetAll();
            USMSTempleteInfo obj= list.Find(e => e.Tkey.ToLower() == Tkey.ToString().ToLower());
            if (obj == null)
                return new USMSTempleteInfo();
            return obj;
        }
        /// <summary>
        /// 根据KEY获取内容
        /// </summary>
        /// <param name="Tkey"></param>
        /// <returns></returns>
        public static USMSTempleteInfo GetByKey(string Tkey)
        {
            List<USMSTempleteInfo> list = GetAll();
            
            USMSTempleteInfo obj = list.Find(e => e.Tkey.ToLower() == Tkey.ToString().ToLower());
            if (obj == null)
                return new USMSTempleteInfo();
            return obj;
        }

        #region 标签替换系统
        /// <summary>
        /// 获取短信内容
        /// </summary>
        /// <param name="UWCID">保修卡ID</param>
        /// <param name="TKey">短信模板系统中的TKEY</param>
        /// <returns></returns>
        public static string GetSMSContent(int UWCID, string TKey)
        {
            UWarrantyCardExInfo uwcobj = UWarrantyCardExController.GetByUWCID(UWCID);
            return UWarrantyCardReplaceTagSys(uwcobj, TKey);          
        }
        /// <summary>
        /// 标签
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="smskey"></param>
        /// <returns></returns>
        public static string UWarrantyCardReplaceTagSys(UWarrantyCardExInfo obj, string smskey)
        {
            return UWarrantyCardReplaceTagSys(obj, smskey, 1);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="smskey"></param>
        /// <param name="LinkMan">第几个联系人</param>
        /// <returns></returns>
        public static string UWarrantyCardReplaceTagSys(UWarrantyCardExInfo obj, string smskey, int LinkMan)
        {
            USMSTempleteInfo smsobj = USMSTempleteController.GetByKey(smskey);
            string smscontent = smsobj.SMSContent.Replace(",", "，");

           // string smscontent = SMSContent;
            if (LinkMan == 1)
            {               
                smscontent = smscontent.Replace("[客户名称]", obj.CustomerName);               
                smscontent = smscontent.Replace("[客户手机]", obj.CustomerMobile);
            }
            if (LinkMan == 2)
            {
                smscontent = smscontent.Replace("[客户名称]", obj.CustomerName2);
                smscontent = smscontent.Replace("[客户手机]", obj.CustomerMobile2);
            }
            if (LinkMan == 3)
            {
                smscontent = smscontent.Replace("[客户名称]", obj.CustomerName3);
                smscontent = smscontent.Replace("[客户手机]", obj.CustomerMobile3);
            }
            smscontent = smscontent.Replace("[交机日期]", obj.IntoDate.ToShortDateString());
            smscontent = smscontent.Replace("[机型]", obj.MachineType);
            smscontent = smscontent.Replace("[机号]", obj.SerialNumber);
            smscontent = smscontent.Replace("[经销商]", obj.AgentName);
            smscontent = smscontent.Replace("[服务电话]", obj.AgentTel);
            smscontent = smscontent.Replace("[交机服务人员]", obj.AgentSvrPer);
            smscontent = smscontent.Replace("[交机人电话]", obj.AgentSvrPerTel);
            return smscontent;
        }
        #endregion
    }
}
